package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;

import net.miginfocom.swing.MigLayout;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class ObjP extends JPanel {

	private JPanel photoNorth = new JPanel();
	private JPanel photoSouth = new JPanel();
	private JButton upload1N = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload2N = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload3N = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload4N = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));

	private String lastPath = "";
	private String pathIm1N = "";
	private String pathIm2N = "";
	private String pathIm3N = "";
	private String pathIm4N = "";
	private String sourceIm1N = "";
	private String sourceIm2N = "";
	private String sourceIm3N = "";
	private String sourceIm4N = "";
	private String sufixIm1N = "";
	private String sufixIm2N = "";
	private String sufixIm3N = "";
	private String sufixIm4N = "";

	private JButton upload1S = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload2S = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload3S = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));
	private JButton upload4S = new JButton(new ImageIcon(
			ClassLoader.getSystemResource("bupload.jpg")));

	private String pathIm1S = "";
	private String pathIm2S = "";
	private String pathIm3S = "";
	private String pathIm4S = "";
	private String sourceIm1S = "";
	private String sourceIm2S = "";
	private String sourceIm3S = "";
	private String sourceIm4S = "";
	private String sufixIm1S = "";
	private String sufixIm2S = "";
	private String sufixIm3S = "";
	private String sufixIm4S = "";

	private int fotoNorth = 0;
	private int fotoSouth = 0;

	private JPanel rilievi = new JPanel();
	private JTextPane puntoCritico = new JTextPane();
	private JTextPane possibileCausa = new JTextPane();
	private JTextPane possibileSoluzione = new JTextPane();

	private String directoryProject;
	private int p;

	protected void paintComponent(Graphics g) {
		setOpaque(false);
		g.drawImage(
				(new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg")))
						.getImage(), 0, 0, null);
		super.paintComponent(g);
	}

	public ObjP(final StartingWindow st, final int p) {
		super();
		this.p = p;
		Properties pc = System.getProperties();
		String homePath = pc.getProperty("user.home");
		directoryProject = homePath + pc.getProperty("file.separator")
				+ "ArchimedeSuper" + pc.getProperty("file.separator")
				+ st.getCount() + pc.getProperty("file.separator");

		lastPath = st.getPath();
		upload1N.setBorder(null);
		upload1N.setContentAreaFilled(false);
		upload1N.setRolloverEnabled(true);
		upload1N.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload1N.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload1N);
				String sourcePath = chooser.getSelectedFile().toString();

				st.setPath(sourcePath);
				lastPath = sourcePath;

				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm1N = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "1pnorth" + p + "."
						+ sufix;
				pathIm1N = destPath;
				sufixIm1N = sufix;

				upload1N.setIcon(tmp);

				photoNorth.add(upload2N, "cell 1 0");
				fotoNorth++;
				st.refresh();
			}
		});

		upload2N.setBorder(null);
		upload2N.setContentAreaFilled(false);
		upload2N.setRolloverEnabled(true);
		upload2N.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload2N.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload2N);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm2N = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "2pnorth" + p + "."
						+ sufix;
				pathIm2N = destPath;
				sufixIm2N = sufix;

				upload2N.setIcon(tmp);
				fotoNorth++;
				if (fotoSouth < 3)
					photoNorth.add(upload3N, "cell 2 0");

				st.refresh();
			}
		});

		upload3N.setBorder(null);
		upload3N.setContentAreaFilled(false);
		upload3N.setRolloverEnabled(true);
		upload3N.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload3N.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (fotoSouth == 0) {
					JFileChooser chooser = new JFileChooser();
					ImagePreviewPanel preview = new ImagePreviewPanel();
					chooser.setAccessory(preview);
					chooser.addPropertyChangeListener(preview);
					lastPath = st.getPath();
					if (lastPath.equals(""))
						chooser.getCurrentDirectory();
					else
						chooser.setCurrentDirectory(new File(lastPath));
					chooser.showOpenDialog(upload3N);
					String sourcePath = chooser.getSelectedFile().toString();
					st.setPath(sourcePath);
					lastPath = sourcePath;
					StringTokenizer tok = new StringTokenizer(sourcePath, ".");
					String sufix = "";
					while (tok.hasMoreTokens())
						sufix = tok.nextToken();

					sourceIm3N = sourcePath;
					ImageIcon tmp = new ImageIcon(sourcePath);
					tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
							Image.SCALE_AREA_AVERAGING));

					String destPath = directoryProject + "3pnorth" + p + "."
							+ sufix;
					pathIm3N = destPath;
					sufixIm3N = sufix;

					upload3N.setIcon(tmp);
					photoSouth.removeAll();

					photoNorth.add(upload4N, "cell 3 0");
					fotoNorth++;
					st.refresh();
				} else {
					JOptionPane.showMessageDialog(null,
							"Rimuovere foto sottostante");
				}
			}
		});

		upload4N.setBorder(null);
		upload4N.setContentAreaFilled(false);
		upload4N.setRolloverEnabled(true);
		upload4N.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload4N.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload4N);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm4N = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "4pnorth" + p + "."
						+ sufix;
				pathIm4N = destPath;
				sufixIm4N = sufix;
				fotoNorth++;
				upload4N.setIcon(tmp);

				st.refresh();
			}
		});

		upload1S.setBorder(null);
		upload1S.setContentAreaFilled(false);
		upload1S.setRolloverEnabled(true);
		upload1S.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload1S.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload1S);
				String sourcePath = chooser.getSelectedFile().toString();

				st.setPath(sourcePath);
				lastPath = sourcePath;

				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm1S = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "1psouth" + p + "."
						+ sufix;
				pathIm1S = destPath;
				sufixIm1S = sufix;

				upload1S.setIcon(tmp);
				fotoSouth++;
				photoSouth.add(upload2S, "cell 1 0");
				st.refresh();
			}
		});

		upload2S.setBorder(null);
		upload2S.setContentAreaFilled(false);
		upload2S.setRolloverEnabled(true);
		upload2S.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload2S.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload2S);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm2S = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "2psouth" + p + "."
						+ sufix;
				pathIm2S = destPath;
				sufixIm2S = sufix;

				upload2S.setIcon(tmp);
				fotoSouth++;
				if (fotoNorth < 3)
					photoSouth.add(upload3S, "cell 2 0");

				st.refresh();

			}
		});

		upload3S.setBorder(null);
		upload3S.setContentAreaFilled(false);
		upload3S.setRolloverEnabled(true);
		upload3S.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload3S.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (fotoNorth == 0) {
					JFileChooser chooser = new JFileChooser();
					ImagePreviewPanel preview = new ImagePreviewPanel();
					chooser.setAccessory(preview);
					chooser.addPropertyChangeListener(preview);
					lastPath = st.getPath();
					if (lastPath.equals(""))
						chooser.getCurrentDirectory();
					else
						chooser.setCurrentDirectory(new File(lastPath));
					chooser.showOpenDialog(upload3S);
					String sourcePath = chooser.getSelectedFile().toString();
					st.setPath(sourcePath);
					lastPath = sourcePath;
					StringTokenizer tok = new StringTokenizer(sourcePath, ".");
					String sufix = "";
					while (tok.hasMoreTokens())
						sufix = tok.nextToken();

					sourceIm3S = sourcePath;
					ImageIcon tmp = new ImageIcon(sourcePath);
					tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
							Image.SCALE_AREA_AVERAGING));

					String destPath = directoryProject + "3psouth" + p + "."
							+ sufix;
					pathIm3S = destPath;
					sufixIm3S = sufix;

					upload3S.setIcon(tmp);
					photoSouth.removeAll();
					photoSouth.add(upload4S, "cell 3 0");
					fotoSouth++;
				} else {
					JOptionPane.showMessageDialog(null,
							"Rimuovere foto sottostante");
				}
				st.refresh();
			}
		});

		upload4S.setBorder(null);
		upload4S.setContentAreaFilled(false);
		upload4S.setRolloverEnabled(true);
		upload4S.setRolloverIcon(new ImageIcon(ClassLoader
				.getSystemResource("upload_icon.jpg")));
		upload4S.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser chooser = new JFileChooser();
				ImagePreviewPanel preview = new ImagePreviewPanel();
				chooser.setAccessory(preview);
				chooser.addPropertyChangeListener(preview);
				lastPath = st.getPath();
				if (lastPath.equals(""))
					chooser.getCurrentDirectory();
				else
					chooser.setCurrentDirectory(new File(lastPath));
				chooser.showOpenDialog(upload4S);
				String sourcePath = chooser.getSelectedFile().toString();
				st.setPath(sourcePath);
				lastPath = sourcePath;
				StringTokenizer tok = new StringTokenizer(sourcePath, ".");
				String sufix = "";
				while (tok.hasMoreTokens())
					sufix = tok.nextToken();

				sourceIm4S = sourcePath;
				ImageIcon tmp = new ImageIcon(sourcePath);
				tmp.setImage(tmp.getImage().getScaledInstance(250, 200,
						Image.SCALE_AREA_AVERAGING));

				String destPath = directoryProject + "4psouth" + p + "."
						+ sufix;
				pathIm4S = destPath;
				sufixIm4S = sufix;

				upload4S.setIcon(tmp);
				fotoSouth++;
				st.refresh();
			}
		});

		ClipboardPopupMenu pcClip = ClipboardPopupMenu
				.installForComponent(puntoCritico);
		pcClip.addCopyFunction("Copia");
		pcClip.addCutFunction("Taglia");
		pcClip.addPasteFunction("Incolla");

		ClipboardPopupMenu psClip = ClipboardPopupMenu
				.installForComponent(possibileSoluzione);
		psClip.addCopyFunction("Copia");
		psClip.addCutFunction("Taglia");
		psClip.addPasteFunction("Incolla");

		ClipboardPopupMenu cb = ClipboardPopupMenu
				.installForComponent(possibileCausa);
		cb.addCopyFunction("Copia");
		cb.addCutFunction("Taglia");
		cb.addPasteFunction("Incolla");

		MouseAdapter ml1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoNorth--;
						if (!pathIm2N.equals("")) {
							pathIm1N = pathIm2N.replace("2p", "1p");
							sufixIm1N = sufixIm2N;
							sourceIm1N = sourceIm2N;
							upload1N.setIcon(upload2N.getIcon());

							if (!pathIm3N.equals("")) {
								pathIm2N = pathIm3N.replace("3p", "2p");
								sufixIm2N = sufixIm3N;
								sourceIm2N = sourceIm3N;
								upload2N.setIcon(upload3N.getIcon());
								if (!pathIm4N.equals("")) {
									pathIm3N = pathIm4N.replace("4p", "3p");
									sufixIm3N = sufixIm4N;
									sourceIm3N = sourceIm4N;
									pathIm4N = "";
									sufixIm4N = "";
									sourceIm4N = "";
									upload3N.setIcon(upload4N.getIcon());
									upload4N.setIcon(new ImageIcon(ClassLoader
											.getSystemResource("bupload.jpg")));
								} else {
									upload3N.setIcon(new ImageIcon(ClassLoader
											.getSystemResource("bupload.jpg")));
									pathIm3N = "";
									sufixIm3N = "";
									sourceIm3N = "";
									try {
										photoNorth.remove(upload4N);
									} catch (Exception noRemove) {
										System.out.println("no remove");
									}
									photoSouth.add(upload1S, "cell 0 0");
								}
							} else {
								upload2N.setIcon(new ImageIcon(ClassLoader
										.getSystemResource("bupload.jpg")));
								pathIm2N = "";
								sufixIm2N = "";
								sourceIm2N = "";
								try {
									photoNorth.remove(upload3N);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}

							}
						} else {
							pathIm1N = "";
							sufixIm1N = "";
							sourceIm1N = "";
							upload1N.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
							try {
								photoNorth.remove(upload2N);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
						}
						st.refresh();
					}
				}
			}
		};
		upload1N.addMouseListener(ml1);
		MouseAdapter ml2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoNorth--;
						if (!pathIm3N.equals("")) {
							pathIm2N = pathIm3N.replace("3p", "2p");
							sufixIm2N = sufixIm3N;
							sourceIm2N = sourceIm3N;
							upload2N.setIcon(upload3N.getIcon());
							if (!pathIm4N.equals("")) {
								pathIm3N = pathIm4N.replace("4p", "3p");
								sufixIm3N = sufixIm4N;
								sourceIm3N = sourceIm4N;
								pathIm4N = "";
								sufixIm4N = "";
								sourceIm4N = "";
								upload3N.setIcon(upload4N.getIcon());
								upload4N.setIcon(new ImageIcon(ClassLoader
										.getSystemResource("bupload.jpg")));
							} else {
								upload3N.setIcon(new ImageIcon(ClassLoader
										.getSystemResource("bupload.jpg")));
								pathIm3N = "";
								sufixIm3N = "";
								sourceIm3N = "";
								try {
									photoNorth.remove(upload4N);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}
								photoSouth.add(upload1S, "cell 0 0");
							}
						} else {
							upload2N.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
							pathIm2N = "";
							sufixIm2N = "";
							sourceIm2N = "";
							try {
								photoNorth.remove(upload3N);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}

						}
						st.refresh();
					}
				}
			}
		};
		upload2N.addMouseListener(ml2);
		MouseAdapter ml3 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoNorth--;
						if (!pathIm4N.equals("")) {
							pathIm3N = pathIm4N.replace("4p", "3p");
							sufixIm3N = sufixIm4N;
							sourceIm3N = sourceIm4N;
							pathIm4N = "";
							sufixIm4N = "";
							sourceIm4N = "";
							upload3N.setIcon(upload4N.getIcon());
							upload4N.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
						} else {
							upload3N.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
							pathIm3N = "";
							sufixIm3N = "";
							sourceIm3N = "";
							try {
								photoNorth.remove(upload4N);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
							photoSouth.add(upload1S, "cell 0 0");
						}
						st.refresh();
					}

				}
			}
		};
		upload3N.addMouseListener(ml3);
		MouseAdapter ml4 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoNorth--;
						upload4N.setIcon(new ImageIcon(ClassLoader
								.getSystemResource("bupload.jpg")));
						pathIm4N = "";
						sufixIm4N = "";
						sourceIm4N = "";
						st.refresh();
					}

				}
			}
		};
		upload4N.addMouseListener(ml4);

		MouseAdapter mlS1 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoSouth--;
						if (!pathIm2S.equals("")) {
							pathIm1S = pathIm2S.replace("2p", "1p");
							sufixIm1S = sufixIm2S;
							sourceIm1S = sourceIm2S;
							upload1S.setIcon(upload2S.getIcon());

							if (!pathIm3S.equals("")) {
								pathIm2S = pathIm3S.replace("3p", "2p");
								sufixIm2S = sufixIm3S;
								sourceIm2S = sourceIm3S;
								upload2S.setIcon(upload3S.getIcon());
								if (!pathIm4S.equals("")) {
									pathIm3S = pathIm4S.replace("4p", "3p");
									sufixIm3S = sufixIm4S;
									sourceIm3S = sourceIm4S;
									pathIm4S = "";
									sufixIm4S = "";
									sourceIm4S = "";
									upload3S.setIcon(upload4S.getIcon());
									upload4S.setIcon(new ImageIcon(ClassLoader
											.getSystemResource("bupload.jpg")));
								} else {
									upload3S.setIcon(new ImageIcon(ClassLoader
											.getSystemResource("bupload.jpg")));
									pathIm3S = "";
									sufixIm3S = "";
									sourceIm3S = "";
									try {
										photoSouth.remove(upload4S);
									} catch (Exception noRemove) {
										System.out.println("no remove");
									}
									photoNorth.add(upload1N, "cell 0 0");
								}
							} else {
								upload2S.setIcon(new ImageIcon(ClassLoader
										.getSystemResource("bupload.jpg")));
								pathIm2S = "";
								sufixIm2S = "";
								sourceIm2S = "";
								try {
									photoSouth.remove(upload3S);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}
							}
						} else {
							pathIm1S = "";
							sufixIm1S = "";
							sourceIm1S = "";
							upload1S.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
							try {
								photoSouth.remove(upload2S);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
						}
						st.refresh();
					}
				}
			}
		};
		upload1S.addMouseListener(mlS1);
		MouseAdapter mlS2 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoSouth--;
						if (!pathIm3S.equals("")) {
							pathIm2S = pathIm3S.replace("3p", "2p");
							sufixIm2S = sufixIm3S;
							sourceIm2S = sourceIm3S;
							upload2S.setIcon(upload3S.getIcon());
							if (!pathIm4S.equals("")) {
								pathIm3S = pathIm4S.replace("4p", "3p");
								sufixIm3S = sufixIm4S;
								sourceIm3S = sourceIm4S;
								pathIm4S = "";
								sufixIm4S = "";
								sourceIm4S = "";
								upload3S.setIcon(upload4S.getIcon());
								upload4S.setIcon(new ImageIcon(ClassLoader
										.getSystemResource("bupload.jpg")));
							} else {
								upload3S.setIcon(new ImageIcon(ClassLoader
										.getSystemResource("bupload.jpg")));
								pathIm3S = "";
								sufixIm3S = "";
								sourceIm3S = "";
								try {
									photoSouth.remove(upload4S);
								} catch (Exception noRemove) {
									System.out.println("no remove");
								}
								photoNorth.add(upload1N, "cell 0 0");
							}
						} else {
							upload2S.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
							pathIm2S = "";
							sufixIm2S = "";
							sourceIm2S = "";
							try {
								photoSouth.remove(upload3S);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}

						}
						st.refresh();
					}
				}
			}
		};
		upload2S.addMouseListener(mlS2);
		MouseAdapter mlS3 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoSouth--;
						if (!pathIm4S.equals("")) {
							pathIm3S = pathIm4S.replace("4p", "3p");
							sufixIm3S = sufixIm4S;
							sourceIm3S = sourceIm4S;
							pathIm4S = "";
							sufixIm4S = "";
							sourceIm4S = "";
							upload3S.setIcon(upload4S.getIcon());
							upload4S.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
						} else {
							upload3S.setIcon(new ImageIcon(ClassLoader
									.getSystemResource("bupload.jpg")));
							pathIm3S = "";
							sufixIm3S = "";
							sourceIm3S = "";
							try {
								photoSouth.remove(upload4S);
							} catch (Exception noRemove) {
								System.out.println("no remove");
							}
							photoNorth.add(upload1N, "cell 0 0");
						}
						st.refresh();
					}
				}
			}
		};
		upload3S.addMouseListener(mlS3);
		MouseAdapter mlS4 = new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON3) {
					int res = JOptionPane.showConfirmDialog(null,
							"Eliminare foto?", "Attenzione", 0);
					if (res == 0) {
						fotoSouth--;
						upload4S.setIcon(new ImageIcon(ClassLoader
								.getSystemResource("bupload.jpg")));
						pathIm4S = "";
						sufixIm4S = "";
						sourceIm4S = "";
						st.refresh();
					}

				}
			}
		};
		upload4S.addMouseListener(mlS4);

		photoNorth.setLayout(new MigLayout());
		photoNorth.setOpaque(false);
		photoNorth.add(upload1N, "cell 0 0");

		photoSouth.setLayout(new MigLayout());
		photoSouth.setOpaque(false);
		photoSouth.add(upload1S, "cell 0 0");

		rilievi.setLayout(new MigLayout());
		rilievi.setOpaque(false);
		JLabel critico = new JLabel("P." + p + ".1 Punto critico");
		critico.setFont(new Font("Arial", Font.BOLD, 20));
		JLabel causa = new JLabel("P." + p + ".2 Possibile causa");
		causa.setFont(new Font("Arial", Font.BOLD, 20));
		JLabel soluzione = new JLabel("P." + p + ".3 Possibile soluzione");
		soluzione.setFont(new Font("Arial", Font.BOLD, 20));

		puntoCritico.setFont(new Font("Arial", Font.PLAIN, 16));
		possibileCausa.setFont(new Font("Arial", Font.PLAIN, 16));
		possibileSoluzione.setFont(new Font("Arial", Font.PLAIN, 16));
		puntoCritico.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		possibileCausa.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		possibileSoluzione
				.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		puntoCritico.setPreferredSize(new Dimension(700, 200));
		possibileCausa.setPreferredSize(new Dimension(700, 200));
		possibileSoluzione.setPreferredSize(new Dimension(700, 200));

		rilievi.add(causa, "wrap");
		rilievi.add(possibileCausa, "wrap");
		rilievi.add(soluzione, "wrap");
		rilievi.add(possibileSoluzione, "wrap");

		JPanel criticoPanel = new JPanel();
		criticoPanel.setLayout(new MigLayout());
		criticoPanel.setOpaque(false);
		criticoPanel.add(critico, "dock north");
		criticoPanel.add(puntoCritico, "dock north");

		setLayout(new MigLayout("", "", "25[]10[]10[]10[]"));

		add(criticoPanel, " gapleft 15 ,wrap");
		add(photoNorth, " gapleft 15 , wrap");
		add(rilievi, " gapleft 15, wrap");

		add(photoSouth, " gapleft 15, wrap");

	}

	public String print() {
		String print = "";
		print = print + "\n#" + pathIm1N;
		print = print + "\n#" + pathIm2N;
		print = print + "\n#" + pathIm3N;
		print = print + "\n#" + pathIm4N;
		print = print + "\n#" + pathIm1S;
		print = print + "\n#" + pathIm2S;
		print = print + "\n#" + pathIm3S;
		print = print + "\n#" + pathIm4S;
		print = print + "\n#start";
		print = print + "\n" + puntoCritico.getText();
		print = print + "\n#END";
		print = print + "\n#start";
		print = print + "\n" + possibileCausa.getText();
		print = print + "\n#END";
		print = print + "\n#start";
		print = print + "\n" + possibileSoluzione.getText();
		print = print + "\n#END";
		print = print + "\n#STOP";
		save();
		return print;
	}

	public void setElement(String data) {
		Scanner c = new Scanner(data);
		// System.out.println("PPPPPPPPPPPPPPPPPPPPPPPP " + data);
		// photo north
		pathIm1N = c.nextLine().substring(1);
		if (pathIm1N.contains("pnorth")) {
			pathIm1N = directoryProject + "1pnorth" + p + "."
					+ pathIm1N.substring(pathIm1N.indexOf(".") + 1);
			upload1N.setIcon(new ImageIcon(pathIm1N));
			photoNorth.add(upload2N, "cell 1 0");
			fotoNorth++;
		} else
			pathIm1N = "";
		pathIm2N = c.nextLine().substring(1);
		if (pathIm2N.contains("pnorth")) {
			pathIm2N = directoryProject + "2pnorth" + p + "."
					+ pathIm2N.substring(pathIm2N.indexOf(".") + 1);
			upload2N.setIcon(new ImageIcon(pathIm2N));
			photoNorth.add(upload3N, "cell 2 0");
			fotoNorth++;
		} else
			pathIm2N = "";
		pathIm3N = c.nextLine().substring(1);
		if (pathIm3N.contains("pnorth")) {
			pathIm3N = directoryProject + "3pnorth" + p + "."
					+ pathIm3N.substring(pathIm3N.indexOf(".") + 1);
			upload3N.setIcon(new ImageIcon(pathIm3N));
			photoNorth.add(upload4N, "cell 3 0");
			fotoNorth++;
			photoSouth.removeAll();
		} else
			pathIm3N = "";
		pathIm4N = c.nextLine().substring(1);
		if (pathIm4N.contains("pnorth")) {
			pathIm4N = directoryProject + "4pnorth" + p + "."
					+ pathIm4N.substring(pathIm4N.indexOf(".") + 1);
			upload4N.setIcon(new ImageIcon(pathIm4N));
			fotoNorth++;
		} else
			pathIm4N = "";
		// photo south
		pathIm1S = c.nextLine().substring(1);
		if (pathIm1S.contains("psouth")) {
			pathIm1S = directoryProject + "1psouth" + p + "."
					+ pathIm1S.substring(pathIm1S.indexOf(".") + 1);
			upload1S.setIcon(new ImageIcon(pathIm1S));
			photoSouth.add(upload2S, "cell 1 0");
			fotoSouth++;
		} else
			pathIm1S = "";
		pathIm2S = c.nextLine().substring(1);
		if (pathIm2S.contains("psouth")) {
			pathIm2S = directoryProject + "2psouth" + p + "."
					+ pathIm2S.substring(pathIm2S.indexOf(".") + 1);
			upload2S.setIcon(new ImageIcon(pathIm2S));
			photoSouth.add(upload3S, "cell 2 0");
			fotoSouth++;
		} else
			pathIm2S = "";
		pathIm3S = c.nextLine().substring(1);
		if (pathIm3S.contains("psouth")) {
			pathIm3S = directoryProject + "3psouth" + p + "."
					+ pathIm3S.substring(pathIm3S.indexOf(".") + 1);
			upload3S.setIcon(new ImageIcon(pathIm3S));
			photoSouth.add(upload4S, "cell 3 0");
			fotoSouth++;
			photoNorth.removeAll();
		} else
			pathIm3S = "";
		pathIm4S = c.nextLine().substring(1);
		if (pathIm4S.contains("psouth")) {
			pathIm4S = directoryProject + "4psouth" + p + "."
					+ pathIm4S.substring(pathIm4S.indexOf(".") + 1);
			upload4S.setIcon(new ImageIcon(pathIm4S));
			fotoSouth++;
		} else
			pathIm4S = "";
		boolean stop = false;
		c.nextLine();
		String testo = c.nextLine();
		while (c.hasNextLine() && !stop) {
			String tmp = c.nextLine();
			if (tmp.contains("\t"))
				tmp.replace("\t", "     ");
			if (tmp.equalsIgnoreCase("#end")) {
				puntoCritico.setText(testo);
				stop = true;
			}
			testo = testo + "\n" + tmp;
		}

		stop = false;
		c.nextLine();
		testo = c.nextLine();
		while (c.hasNextLine() && !stop) {
			String tmp = c.nextLine();
			if (tmp.contains("\t"))
				tmp.replace("\t", "     ");
			if (tmp.equalsIgnoreCase("#end")) {
				possibileCausa.setText(testo);
				stop = true;
			}
			testo = testo + "\n" + tmp;
		}
		stop = false;
		c.nextLine();
		testo = c.nextLine();
		while (c.hasNextLine() && !stop) {
			String tmp = c.nextLine();
			if (tmp.contains("\t"))
				tmp.replace("\t", "     ");
			if (tmp.equalsIgnoreCase("#end")) {
				possibileSoluzione.setText(testo);
				stop = true;
			}
			testo = testo + "\n" + tmp;
		}

	}

	public void resize(File in, int w, int h, String formato, String out)
			throws IOException {
		Image newImg = ImageIO.read(in).getScaledInstance(w, h,
				Image.SCALE_SMOOTH);
		BufferedImage bim = new java.awt.image.BufferedImage(w, h,
				BufferedImage.TYPE_INT_RGB);
		bim.createGraphics().drawImage(newImg, 0, 0, null);
		FileOutputStream fos = new FileOutputStream(out);
		ImageIO.write(bim, formato, fos);
		fos.close();
	}

	public void save() {

		try {
			if (!sourceIm1N.equals(""))
				resize(new File(sourceIm1N), 250, 200, sufixIm1N, pathIm1N);
			if (!sourceIm2N.equals(""))
				resize(new File(sourceIm2N), 250, 200, sufixIm2N, pathIm2N);
			if (!sourceIm3N.equals(""))
				resize(new File(sourceIm3N), 250, 200, sufixIm3N, pathIm3N);
			if (!sourceIm4N.equals(""))
				resize(new File(sourceIm4N), 250, 200, sufixIm4N, pathIm4N);

			if (!sourceIm1S.equals(""))
				resize(new File(sourceIm1S), 250, 200, sufixIm1S, pathIm1S);
			if (!sourceIm2S.equals(""))
				resize(new File(sourceIm2S), 250, 200, sufixIm2S, pathIm2S);
			if (!sourceIm3S.equals(""))
				resize(new File(sourceIm3S), 250, 200, sufixIm3S, pathIm3S);
			if (!sourceIm4S.equals(""))
				resize(new File(sourceIm4S), 250, 200, sufixIm4S, pathIm4S);
		} catch (IOException e) {
			System.out.println("no resize");
			e.printStackTrace();

		}
	}

	public Document pdf(Document doc, int n) {

		Paragraph dati;
		dati = new Paragraph("Punto P" + n, FontFactory.getFont(
				FontFactory.TIMES, 18));
		dati.setAlignment(Element.ALIGN_LEFT);
		try {
			doc.add(dati);
		} catch (DocumentException e) {
			e.printStackTrace();
		}

		PdfPTable table = new PdfPTable(1);
		table.setWidthPercentage(100);

		Phrase testo;
		PdfPCell cos;

		// punto cirtico
		testo = new Phrase("P." + n + ".1 Punto critico :\n"
				+ puntoCritico.getText(), FontFactory.getFont(
				FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		table.addCell(cos);

		try {
			table.setSpacingAfter(10);
			table.setSpacingBefore(5);
			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// foto north

		int col = 0;

		if (!pathIm1N.equals(""))
			col++;
		if (!pathIm2N.equals(""))
			col++;
		if (!pathIm3N.equals(""))
			col++;
		if (!pathIm4N.equals(""))
			col++;

		table = new PdfPTable(2);
		table.setWidthPercentage(100);
		if (!pathIm1N.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm1N);
				img.setCompressionLevel(3);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				cos = new PdfPCell(img);
				if (col == 1)
					cos.setColspan(2);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm2N.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm2N);
				img.setCompressionLevel(3);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm3N.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm3N);
				img.setCompressionLevel(3);
				dati = new Paragraph("");
				PdfPCell space = new PdfPCell(dati);
				space.setBorder(PdfPCell.NO_BORDER);
				table.addCell(space);
				table.addCell(space);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				cos = new PdfPCell(img);
				if (col == 3)
					cos.setColspan(2);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm4N.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm4N);
				img.setCompressionLevel(3);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		try {
			table.setSpacingAfter(10);
			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// possibile causa
		table = new PdfPTable(1);
		table.setWidthPercentage(100);

		testo = new Phrase("P." + n + ".2 Possibile causa :\n"
				+ possibileCausa.getText(), FontFactory.getFont(
				FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		table.addCell(cos);

		// possibile soluzione
		testo = new Phrase("P." + n + ".3 Possibile soluzione :\n"
				+ possibileSoluzione.getText(), FontFactory.getFont(
				FontFactory.TIMES, 11));
		cos = new PdfPCell(testo);
		table.addCell(cos);

		try {
			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// spazio
		dati = new Paragraph("");
		dati.setSpacingAfter(20);
		try {
			doc.add(dati);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// foto south
		col = 0;

		if (!pathIm1S.equals(""))
			col++;
		if (!pathIm2S.equals(""))
			col++;
		if (!pathIm3S.equals(""))
			col++;
		if (!pathIm4S.equals(""))
			col++;

		table = new PdfPTable(2);
		table.setWidthPercentage(100);
		if (!pathIm1S.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm1S);
				img.setCompressionLevel(3);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				cos = new PdfPCell(img);
				if (col == 1)
					cos.setColspan(2);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm2S.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm2S);
				img.setCompressionLevel(3);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm3S.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm3S);
				img.setCompressionLevel(3);
				dati = new Paragraph("");
				PdfPCell space = new PdfPCell(dati);
				space.setBorder(PdfPCell.NO_BORDER);
				table.addCell(space);
				table.addCell(space);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				cos = new PdfPCell(img);
				if (col == 3)
					cos.setColspan(2);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (!pathIm4S.equals(""))
			try {
				com.itextpdf.text.Image img = com.itextpdf.text.Image
						.getInstance(pathIm4S);
				img.setCompressionLevel(3);
				if (col < 2)
					img.scaleAbsolute(250f, 200f);
				else
					img.scaleAbsolute(235f, 165f);
				cos = new PdfPCell(img);
				cos.setHorizontalAlignment(Element.ALIGN_CENTER);
				cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
				cos.setBorder(PdfPCell.NO_BORDER);
				table.addCell(cos);
			} catch (BadElementException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		try {
			table.setSpacingAfter(10);
			doc.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return doc;
	}
}
